package com.hc.mall.thirdpart.controller;

import com.hc.common.utils.R;
import com.hc.mall.product.entity.BrandEntity;
import com.hc.mall.thirdpart.feign.BrandFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("common")
public class FileUploadController {
    @Value("${file.uploadDir}")
    private String uploadDir;
    @Autowired
    private ServletContext servletContext;
    @Autowired
    private BrandFeign brandFeign;
    @RequestMapping(value = "/fileUpload/{brandId}",method = RequestMethod.POST)
    public R fileUpload(@RequestBody MultipartFile file, @PathVariable("brandId") Long id){
        if(file.isEmpty()){
            return R.error("文件不存在");
        }
        String destName = "";
        try {
            String originalFilename = file.getOriginalFilename();
            Path uploadPath = Paths.get(uploadDir);
            if(!Files.exists(uploadPath)){
                Files.createDirectories(uploadPath);
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(new Date());
            destName = date + "-" + UUID.randomUUID() + originalFilename;
            String newName = uploadDir + destName;
            Path filePath = uploadPath.resolve(newName);
            file.transferTo(filePath);
            BrandEntity brandEntity = new BrandEntity();
            brandEntity.setBrandId(id);
            brandEntity.setLogo("/common/filePath/" + destName);
            brandFeign.save(brandEntity);
        }catch (Exception e){
            return R.error("文件上传失败"+e.getMessage());
        }
        return R.ok("/common/filePath/" +destName);
    }

    @RequestMapping(value = "/fileUpload/common",method = RequestMethod.POST)
    public R fileUploadCommon(@RequestBody MultipartFile file){ //文件本地保存通用方法
        if(file.isEmpty()){
            return R.error("文件不存在");
        }
        String destFileFullName = "";
        try {
            String originalFilename = file.getOriginalFilename();
            Path uploadPath = Paths.get(uploadDir);
            if(!Files.exists(uploadPath)){
                Files.createDirectories(uploadPath);
            }
            String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            destFileFullName = date + "-" + UUID.randomUUID() + originalFilename;
            Path filePath = uploadPath.resolve(uploadDir + destFileFullName);
            file.transferTo(filePath);
        }catch (Exception e){
            return R.error("文件上传失败: "+e.getMessage());
        }
        // /common/filePath/和ThirdWebConfig中保持一致,用于静态资源访问过滤
        return R.ok("/common/filePath/" + destFileFullName);
    }
}
